// Evol scripts.
// Author:
//    Travolta
// Description:
//    Trader on the market. One of his pious escaped and
//    flies around the town.
// Variables:
//    ArtisQuests_CatchPiou   -- quest variable
// Quest states:
//    0  -- not started
//    1  -- trader asked to catch piou
//    2  -- quest finished
// Note: this script is ugly like hell

001-1,57,118,0	script	Salem#001-1	NPC_SALEM,{

    function BuyPiou {
        .@price = getarg(0);
        mesq l("The piou costs @@E.", .@price);
        next;
        menuint l("Alright, I take it."), 1,
                l("I changed my mind."), 2;

        mes "";
        mesn;
        if (@menuret == 2)
            return 4;
        if (Zeny < .@price)
        {
            mesq l("Don't you try to trick an old merchant! You don't have enough money, you need @@E", .@price);

            return 1;
        }
        if (!checkweight(4000, 1))
        {
            mesq l("You can't carry another @@? What a shame.", PiouEgg);

            return 2;
        }
        getinventorylist;
        if (.@inventorylist_count >= 100)
        {
            mesq l("You don't have enough room in your backpack for a @@. Get rid of some junk and come back.", PiouEgg);

            return 3;
        }
        Zeny = Zeny - .@price;
        getitem 4000, 1;
        ArtisQuests_PiousBought += 1;
        mesq l("You take good care of your piou. Remember to feed it every day.");

        return 0;
    }

    if (2 == select("[debug]continue quest:[debug] start over"))
    {
        debugmes "Starting quest CatchPiou over";
        setq ArtisQuests_CatchPiou, 0;
    }

    .@q = getq(ArtisQuests_CatchPiou);
    @ArtisQuests_PiouPrice = 30000;

    if (.@q == 1)
    {
        if (.PiouCaught)
        {
        speech 0,
            l("Look who is back..."),
            l("And with my piou! That's wonderful, I can't think at how hard that was to catch it."),
            l("Great, you get your piece of cake, I give you a 90% discount on the @@ of your choice.", getitemlink (4000));
            next;

            @ArtisQuests_PiouPrice = 3000;
            ArtisQuests_PiouDiscount = 1;
            donpcevent strnpcinfo(3) + "::OnPiouFlee";
            setq ArtisQuests_CatchPiou, 2;
            .@q = getq(ArtisQuests_CatchPiou);

            .LastPiouHunter$ = "";
            ArtisQuests_CatchPiou_Difficulcy = 0;
            .@BoughtPiou = BuyPiou(@ArtisQuests_PiouPrice);
            if (.@BoughtPiou == 0)
                ArtisQuests_PiouDiscount = 0;
            else if (.@BoughtPiou == 4)
                speech 1, l("See you next time!");

            close;
        }
        else
        {
            speech 0,
                l("Look who is back..."),
                l("So, where is my piou? You should keep the given promise.");
                next;
            goto L_SalemMenuShop;
        }
    }

    mesq l("Good day, my friend, come closer, come closer!");
    next;
    mesq l("Just look at my goods for sale! Fresh fruits and vegetables were shipped only this morning. And for reasonable price, of course.");
    next;


L_SalemMenuShop:
    menuint
        rif(.@q < 2, l("What about those pious? They look so cute.")), 0,
        rif(.@q != 0, l("I'd like to buy a piou.")), 1,
        l("I'd like to see your other products."), 2;

    mes "";
    mesn;
    
    switch (@menuret)
    {
        case 0:
            goto L_AboutPious;
        case 1:
            if (.@q == 2 && ArtisQuests_PiouDiscount)
            {
                mesq l("You still have a 90% discount on one piou");
                @ArtisQuests_PiouPrice = 3000;
                next;
            }
            if (!BuyPiou(@ArtisQuests_PiouPrice))
                ArtisQuests_PiouDiscount = 0;
            next;
            mesq l("See you next time!");
            close;
        case 2:
            goto L_BuyShop;
    }

L_AboutPious:
    mesq l("Ah, these... Citizens like buying them as pets.");
    next;
    mesq l("With proper training, a piou can become a good friend and faithful companion in your adventures.");
    next;
    mesq l("Their price is usually high, but you know what? You have a lucky day.");
    next;
    mesq l("These little pious here can't fly. At least that's what I thought.");
    next;
    mesq l("One of these little bastards started flying early, and escaped from me.");
    next;
    mesq l("It's still too young to fly too far away, so it just circles nearby.");
    next;
    mesq l("But I can't leave to catch it, so I'm asking you.");
    next;
    mesq l("If you catch the escaped piou and bring it back, I will give you a 90% discount on a piou.");
    next;

    menuint rif (.@q == 0, l("Sounds like easy money. I'll do it.")), 0,
            rif (.@q == 1, l("I'm on my way to bring it back to you!")), 1,
            l("I don't really have time to chase pious, let me just buy one for it's real price (30000E)."), 2,
            l("I don't want to buy a piou right now."), 3;
            
    mes "";
    mesn;

    switch (@menuret)
    {
        case 0:
            goto L_QuestInfo;
        case 1:
            mesq l("Alright, see you later then!");
            close;
        case 2:
            BuyPiou @ArtisQuests_PiouPrice;
            mesq l("See you later!");
            close;
        case 3:
            mesq l("See you later!");
            close;
    }

L_QuestInfo:
    mesq l("The little piou is flying nearby, all you need is to catch it and bring back to me.");
    next;
    setq ArtisQuests_CatchPiou, 1;
    ArtisQuests_CatchPiou_Difficulcy = 2;
    mesq l("Good luck!");
    close;

L_BuyShop:
    mes "not implemented yet!";
    close;

OnPiouFlee:
    sleep 120000;
    if (!.PiouCaught)
        end;
    .PiouCaught = 0;
    .@piou$ = "#FlyingPiou1";
    if (getstrlen(.LastPiouHunter$) > 0)
    {
        message .LastPiouHunter$, .PiouEscapedMessage$;
        .LastPiouHunter$ = "";
    }
    // debugmes "The piou escaped from " + .LastPiouHunter$;
    sleep 60000;
    enablenpc .@piou$;
    movenpc .@piou$, 53, 117;
    donpcevent .@piou$ + "::OnInit";
    end;

OnInit:
    .sex = G_MALE;
    .distance = 5;
    end;
}
